<script setup lang="ts">
import { ref, watch, nextTick } from 'vue'
import { Field, Button, Icon } from 'vant'

defineProps({
  loading: {
    type: Boolean,
    default: false
  },
  disabled: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['search', 'stop'])

const searchText = ref('')
const showModelSelector = ref(false)
const selectedModel = ref('GLM-Z1-Air')

const models = [
  { 
    name: 'GLM-Z1-Air', 
    label: 'GLM-Z1-Air', 
    description: '智谱AI最新模型，响应快速，适合日常搜索查询'
  },
  { 
    name: 'deepseek-r1:14b', 
    label: 'DeepSeek-R1-14B', 
    description: '深度思考模型，逻辑推理能力强，适合复杂搜索'
  }
]

const handleSearch = () => {
  if (!searchText.value.trim()) return
  emit('search', {
    text: searchText.value,
    model: selectedModel.value === 'GLM-Z1-Air' ? undefined : selectedModel.value
  })
  searchText.value = ''
}

const handleStopStream = () => {
  emit('stop')
}

const selectModel = (model: string) => {
  selectedModel.value = model
  showModelSelector.value = false
}

const adjustTextareaHeight = (element: HTMLTextAreaElement) => {
  element.style.height = 'auto'
  const lineHeight = 24
  const maxHeight = lineHeight * 6
  const newHeight = Math.min(element.scrollHeight, maxHeight)
  element.style.height = `${newHeight}px`
}

const handleTextareaInput = (event: Event) => {
  const textarea = event.target as HTMLTextAreaElement
  adjustTextareaHeight(textarea)
}

watch(searchText, () => {
  nextTick(() => {
    const textareaElement = document.querySelector('.search-textarea textarea') as HTMLTextAreaElement
    if (textareaElement) {
      adjustTextareaHeight(textareaElement)
    }
  })
})
</script>

<template>
  <div class="input-area">
    <div class="input-controls">
      <!-- 模型选择按钮 -->
      <div class="model-selector">
        <button 
          class="model-button" 
          @click="showModelSelector = !showModelSelector"
        >
          <Icon name="setting-o" size="16" />
          <span>{{ selectedModel }}</span>
          <Icon name="arrow-up" size="12" :class="{ rotated: !showModelSelector }" />
        </button>
        
        <!-- 上拉菜单 -->
        <div v-if="showModelSelector" class="model-menu">
          <div 
            v-for="model in models" 
            :key="model.name"
            class="model-option"
            :class="{ active: selectedModel === model.name }"
            @click="selectModel(model.name)"
          >
            <div class="model-info">
              <div class="model-name">{{ model.label }}</div>
              <div class="model-desc">{{ model.description }}</div>
            </div>
            <Icon v-if="selectedModel === model.name" name="passed" size="16" color="#4080ff" />
          </div>
        </div>
      </div>

      <Field
        v-model="searchText"
        type="textarea"
        rows="1"
        autosize
        class="search-textarea"
        placeholder="请输入您想要搜索的内容"
        :disabled="disabled || loading"
        @input="handleTextareaInput"
        @keyup.enter.prevent="handleSearch"
      >
        <template #button>
          <Button 
            v-if="!loading"
            class="action-button search-button" 
            size="small" 
            :disabled="!searchText.trim() || disabled" 
            @click="handleSearch"
          >
            <Icon name="search" size="18" />
            <span>搜索</span>
          </Button>
          <Button 
            v-else
            class="action-button stop-button" 
            size="small" 
            @click="handleStopStream"
          >
            <Icon name="close" size="18" />
            <span>停止</span>
          </Button>
        </template>
      </Field>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.input-area {
  padding: 12px 16px;
  background-color: white;
  border-top: 1px solid #eee;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  
  .input-controls {
    display: flex;
    gap: 8px;
    align-items: center;
    
    .model-selector {
      position: relative;
      
      .model-button {
        display: flex;
        align-items: center;
        gap: 6px;
        padding: 8px 12px;
        height: 40px;
        background-color: #f8f9fa;
        color: #606266;
        border: 1px solid #dcdfe6;
        border-radius: 20px;
        font-size: 14px;
        min-width: fit-content;
        white-space: nowrap;
        cursor: pointer;
        transition: all 0.3s ease;
        
        &:hover {
          background-color: #ecf5ff;
          border-color: #b3d8ff;
          color: #409eff;
          box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
        }
        
        &:active {
          transform: translateY(0);
          box-shadow: 0 1px 3px rgba(64, 158, 255, 0.3);
        }
        
        .rotated {
          transform: rotate(180deg);
          transition: transform 0.3s ease;
        }
      }
      
      .model-menu {
        position: absolute;
        bottom: 100%;
        left: 0;
        right: auto;
        width: 310px;
        margin-bottom: 8px;
        background: white;
        border-radius: 12px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
        border: 1px solid #e4e7ed;
        overflow: hidden;
        animation: slideUp 0.3s ease;
        z-index: 100;
        
        .model-option {
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 12px 16px;
          cursor: pointer;
          transition: all 0.2s ease;
          border-bottom: 1px solid #f0f0f0;
          
          &:last-child {
            border-bottom: none;
          }
          
          &:hover {
            background-color: #f5f7fa;
          }
          
          &.active {
            background-color: #ecf5ff;
            color: #409eff;
          }
          
          .model-info {
            flex: 1;
            
            .model-name {
              font-size: 14px;
              font-weight: 500;
              margin-bottom: 2px;
            }
            
            .model-desc {
              font-size: 12px;
              color: #909399;
              line-height: 1.4;
            }
          }
        }
      }
    }
    
    .search-textarea {
      flex: 1;
      background-color: #f8f9fa;
      border-radius: 20px;
      
      :deep(.van-field__body) {
        padding-right: 8px;
      }
      
      :deep(.van-field__control) {
        min-height: 24px;
        max-height: 144px;
        line-height: 24px;
        padding: 8px 0;
        overflow-y: auto;
        resize: none;
        font-size: 15px;
      }
      
      :deep(.van-field__button) {
        align-self: center;
      }
    }
  }
  
  .action-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 8px 16px;
    border-radius: 20px;
    font-weight: 500;
    font-size: 14px;
    
    &.search-button {
      background-color: #4080ff;
      color: white;
      
      &:active {
        background-color: #3070ee;
      }
      
      &[disabled] {
        background-color: #a7c0ff;
        opacity: 0.8;
      }
    }
    
    &.stop-button {
      background-color: #ff4d4f;
      color: white;
      
      &:active {
        background-color: #f5222d;
      }
    }
  }
}

@keyframes slideUp {
  from { 
    opacity: 0; 
    transform: translateY(10px); 
  }
  to { 
    opacity: 1; 
    transform: translateY(0); 
  }
}
</style>
